package controllers

import (
	"chat-room/http/dao"
	"chat-room/http/dto"
	"chat-room/lib"
	"chat-room/providers"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type Room struct {
	Base
}

/**
 * @Author: yang
 * @Description：聊天室页面
 * @Date: 2021/10/20 10:54
 */
func (r *Room) RoomIndex(c *gin.Context) {
	c.HTML(http.StatusOK, "rooms.html", gin.H{})
}

/**
 * @Author: yang
 * @Description：聊天室聊天页
 * @Date: 2021/10/25 17:32
 */
func (r *Room) RoomDetailIndex(c *gin.Context) {
	id := c.Param("id")
	name := c.Param("name")
	token := c.Param("token")
	fmt.Println()
	c.HTML(http.StatusOK, "index.html", gin.H{
		"id":    id,
		"name":  name,
		"token": token,
	})
}

/**
 * @Author: yang
 * @Description：聊天室列表
 * @Date: 2021/10/19 16:14
 */
func (r *Room) GetRoomList(c *gin.Context) {

	// dto 数据绑定和校验
	var form dto.RoomListInput
	if err := c.Bind(&form); err != nil {
		r.BadResponse(10000, err.Error(), c)
		return
	}

	// dao 获取数据
	tx := providers.Mysql
	roomDao := dao.Room{}
	list, _ := roomDao.List(tx, form)

	userDao := dao.User{}
	clist, _ := userDao.Count(tx)

	// 数据重装
	rlist := []dto.RoomDetailInfo{}
	for _, v := range list {
		userNumber := 0
		for _, vc := range clist {
			if v.Id == vc.RoomId {
				userNumber = vc.UserCount
				continue
			}
		}
		rlist = append(rlist, dto.RoomDetailInfo{
			Id:         v.Id,
			Name:       v.Name,
			UserNumber: userNumber,
			CreatedAt:  v.CreatedAt.Format("2006-01-02 15:04:05"),
			Number:     v.Number,
		})
	}

	r.SuccessResponse(dto.RoomListOutput{
		Count: len(rlist),
		List:  rlist,
	}, c)
	return
}

/**
 * @Author: yang
 * @Description：添加聊天室
 * @Date: 2021/10/19 16:15
 */
func (r *Room) RoomAdd(c *gin.Context) {

	// dto 数据绑定和校验
	var form dto.RoomAddInput
	if err := c.Bind(&form); err != nil {
		r.BadResponse(10000, err.Error(), c)
		return
	}
	if err := form.CheckRoomAddInput(); err != nil {
		r.BadResponse(10004, err.Error(), c)
		return
	}

	// dao 数据处理
	roomDao := dao.Room{
		Name:     form.RoomName,
		Number:   strconv.Itoa(lib.GetRandNum(10000)),
		IsDelete: 0,
	}
	if err := roomDao.Save(providers.Mysql); err != nil {
		r.BadResponse(20001, err.Error(), c)
		return
	}

	// 结果返回
	r.SuccessResponse("", c)
	return
}

/**
 * @Author: yang
 * @Description：聊天室详情
 * @Date: 2021/10/19 16:15
 */
func (r *Room) RoomInfo(c *gin.Context) {
	// dto 数据绑定和校验
	var form dto.RoomInfoInput
	if err := c.Bind(&form); err != nil {
		r.BadResponse(10000, err.Error(), c)
		return
	}
	if err := form.CheckRoomInfoInput(); err != nil {
		r.BadResponse(10004, err.Error(), c)
		return
	}

	// dao 数据处理--聊天室
	tx := providers.Mysql
	roomDao := &dao.Room{
		Id:       form.RoomId,
		IsDelete: 0,
	}
	roomInfo, err := roomDao.Find(tx, roomDao)
	if err != nil {
		r.BadResponse(20002, err.Error(), c)
		return
	}

	// dao 数据处理--用户列表
	userDao := &dao.User{
		RoomId:   roomInfo.Id,
		IsDelete: 0,
	}
	userList, err := userDao.List(tx, userDao)

	// dao 数据处理--聊天记录
	logDao := &dao.Log{}
	logList, _, _ := logDao.List(tx, &dto.LogListInput{
		PageSize: 10,
		Page:     1,
		RoomId:   roomInfo.Id,
	})

	// 结果返回
	r.SuccessResponse(dto.RoomDetailOutput{
		RoomInfo: dto.RoomInfo{
			Id:        roomInfo.Id,
			Number:    roomInfo.Number,
			Name:      roomInfo.Name,
			CreatedAt: roomInfo.CreatedAt,
		},
		UserList: userList,
		LogList:  logList,
	}, c)
	return
}
